home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  10.2 KB  |  466 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <math.h>
  29. #include "gltk.h"
  30.  
  31.  
  32. #define VORDER 10
  33. #define CORDER 10
  34. #define TORDER 3
  35.  
  36. #define VMAJOR_ORDER 2
  37. #define VMINOR_ORDER 3
  38.  
  39. #define CMAJOR_ORDER 2
  40. #define CMINOR_ORDER 2
  41.  
  42. #define TMAJOR_ORDER 2
  43. #define TMINOR_ORDER 2
  44.  
  45. #define VDIM 4
  46. #define CDIM 4
  47. #define TDIM 2
  48.  
  49. #define ONE_D 1
  50. #define TWO_D 2
  51.  
  52. #define EVAL 3
  53. #define MESH 4
  54.  
  55.  
  56. GLenum doubleBuffer, directRender;
  57.  
  58. float rotX = 0.0, rotY = 0.0, translateZ = -1.0;
  59.  
  60. GLenum arrayType = ONE_D;
  61. GLenum colorType = GL_FALSE;
  62. GLenum textureType = GL_FALSE;
  63. GLenum polygonFilled = GL_FALSE;
  64. GLenum lighting = GL_FALSE;
  65. GLenum mapPoint = GL_FALSE;
  66. GLenum mapType = EVAL;
  67.  
  68. double point1[10*4] = {
  69.     -0.5, 0.0, 0.0, 1.0,
  70.     -0.4, 0.5, 0.0, 1.0,
  71.     -0.3,-0.5, 0.0, 1.0,
  72.     -0.2, 0.5, 0.0, 1.0,
  73.     -0.1,-0.5, 0.0, 1.0,
  74.     0.0, 0.5, 0.0, 1.0,
  75.     0.1,-0.5, 0.0, 1.0,
  76.     0.2, 0.5, 0.0, 1.0,
  77.     0.3,-0.5, 0.0, 1.0,
  78.     0.4, 0.0, 0.0, 1.0,
  79. };
  80. double cpoint1[10*4] = {
  81.     0.0, 0.0, 1.0, 1.0,
  82.     0.3, 0.0, 0.7, 1.0,
  83.     0.6, 0.0, 0.3, 1.0,
  84.     1.0, 0.0, 0.0, 1.0,
  85.     1.0, 0.3, 0.0, 1.0,
  86.     1.0, 0.6, 0.0, 1.0,
  87.     1.0, 1.0, 0.0, 1.0,
  88.     1.0, 1.0, 0.5, 1.0,
  89.     1.0, 1.0, 1.0, 1.0,
  90. };
  91. double tpoint1[11*4] = {
  92.     0.0, 0.0, 0.0, 1.0,
  93.     0.0, 0.1, 0.0, 1.0,
  94.     0.0, 0.2, 0.0, 1.0,
  95.     0.0, 0.3, 0.0, 1.0,
  96.     0.0, 0.4, 0.0, 1.0,
  97.     0.0, 0.5, 0.0, 1.0,
  98.     0.0, 0.6, 0.0, 1.0,
  99.     0.0, 0.7, 0.0, 1.0,
  100.     0.0, 0.8, 0.0, 1.0,
  101.     0.0, 0.9, 0.0, 1.0,
  102. };
  103. double point2[2*3*4] = {
  104.     -0.5, -0.5,  0.5, 1.0, 
  105.      0.0,  1.0,  0.5, 1.0, 
  106.      0.5, -0.5,  0.5, 1.0,
  107.     -0.5,  0.5, -0.5, 1.0, 
  108.      0.0, -1.0, -0.5, 1.0, 
  109.      0.5,  0.5, -0.5, 1.0,
  110. };
  111. double cpoint2[2*2*4] = {
  112.     0.0, 0.0, 0.0, 1.0,
  113.     0.0, 0.0, 1.0, 1.0,
  114.     0.0, 1.0, 0.0, 1.0,
  115.     1.0, 1.0, 1.0, 1.0,
  116. };
  117. double tpoint2[2*2*2] = {
  118.     0.0, 0.0, 0.0, 1.0,
  119.     1.0, 0.0, 1.0, 1.0, 
  120. };
  121. float textureImage[4*2*4] =  {
  122.     1.0, 1.0, 1.0, 1.0,
  123.     1.0, 0.0, 0.0, 1.0,
  124.     1.0, 0.0, 0.0, 1.0,
  125.     1.0, 1.0, 1.0, 1.0, 
  126.     1.0, 1.0, 1.0, 1.0,
  127.     1.0, 0.0, 0.0, 1.0,
  128.     1.0, 0.0, 0.0, 1.0,
  129.     1.0, 1.0, 1.0, 1.0, 
  130. };
  131.  
  132.  
  133. static void Init(void)
  134. {
  135.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  136.     static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  137.     static float position[] = {0.0, 0.0, -150.0, 0.0};
  138.     static float front_mat_diffuse[] = {1.0, 0.2, 1.0, 1.0};
  139.     static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  140.     static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  141.     static float lmodel_twoside[] = {GL_TRUE};
  142.     static float decal[] = {GL_DECAL};
  143.     static float repeat[] = {GL_REPEAT};
  144.     static float nr[] = {GL_NEAREST};
  145.  
  146.     glFrontFace(GL_CCW);
  147.  
  148.     glEnable(GL_DEPTH_TEST);
  149.  
  150.     glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1);
  151.     glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1);
  152.  
  153.     glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER*VDIM, VMAJOR_ORDER, 0.0,
  154.         1.0, VDIM, VMINOR_ORDER, point2);
  155.     glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER*CDIM, CMAJOR_ORDER, 0.0,
  156.         1.0, CDIM, CMINOR_ORDER, cpoint2);
  157.     glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER*TDIM,
  158.         TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2);
  159.  
  160.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  161.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  162.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  163.     
  164.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  165.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  166.  
  167.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  168.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  169.  
  170.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  171.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  172.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  173.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr);
  174.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr);
  175.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT,
  176.          (GLvoid *)textureImage);
  177. }
  178.  
  179. static void DrawPoints1(void)
  180. {
  181.     GLint i;
  182.  
  183.     glColor3f(0.0, 1.0, 0.0);
  184.     glPointSize(2);
  185.     glBegin(GL_POINTS);
  186.     for (i = 0; i < VORDER; i++) {
  187.         glVertex4dv(&point1[i*4]);
  188.     }
  189.     glEnd();
  190. }
  191.  
  192. static void DrawPoints2(void)
  193. {
  194.     GLint i, j;
  195.  
  196.     glColor3f(1.0, 0.0, 1.0);
  197.     glPointSize(2);
  198.     glBegin(GL_POINTS);
  199.     for (i = 0; i < VMAJOR_ORDER; i++) {
  200.         for (j = 0; j < VMINOR_ORDER; j++) {
  201.         glVertex4dv(&point2[i*4*VMINOR_ORDER+j*4]);
  202.         }
  203.     }
  204.     glEnd();
  205. }
  206.  
  207. static void DrawMapEval1(float du)
  208. {
  209.     float u;
  210.  
  211.     glColor3f(1.0, 0.0, 0.0);
  212.     glBegin(GL_LINE_STRIP);
  213.     for (u = 0.0; u < 1.0; u += du) {
  214.         glEvalCoord1d(u);
  215.     }
  216.     glEvalCoord1d(1.0);
  217.     glEnd();
  218. }
  219.  
  220. static void DrawMapEval2(float du, float dv)
  221. {
  222.     float u, v, tmp;
  223.  
  224.     glColor3f(1.0, 0.0, 0.0);
  225.     for (v = 0.0; v < 1.0; v += dv) {
  226.     glBegin(GL_QUAD_STRIP);
  227.         for (u = 0.0; u <= 1.0; u += du) {
  228.         glEvalCoord2d(u,v);
  229.         tmp = (v + dv < 1.0) ? (v + dv) : 1.0;
  230.         glEvalCoord2d(u, tmp);
  231.         }
  232.         glEvalCoord2d(1.0, v);
  233.         glEvalCoord2d(1.0, v+dv);
  234.     glEnd();
  235.     }
  236. }
  237.  
  238. static void RenderEval(void)
  239. {
  240.  
  241.     if (colorType) {
  242.     glEnable(GL_MAP1_COLOR_4);
  243.     glEnable(GL_MAP2_COLOR_4);
  244.     } else {
  245.     glDisable(GL_MAP1_COLOR_4);
  246.     glDisable(GL_MAP2_COLOR_4);
  247.     }
  248.  
  249.     if (textureType) {
  250.     glEnable(GL_TEXTURE_2D);
  251.     glEnable(GL_MAP2_TEXTURE_COORD_2);
  252.     } else {
  253.     glDisable(GL_TEXTURE_2D);
  254.     glDisable(GL_MAP2_TEXTURE_COORD_2);
  255.     }
  256.  
  257.     if (polygonFilled) {
  258.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  259.     } else {
  260.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  261.     }
  262.  
  263.     glShadeModel(GL_SMOOTH);
  264.  
  265.     switch (mapType) {
  266.       case EVAL:
  267.     switch (arrayType) {
  268.       case ONE_D:
  269.         glDisable(GL_MAP2_VERTEX_4);
  270.         glEnable(GL_MAP1_VERTEX_4);
  271.         DrawPoints1();
  272.         DrawMapEval1(0.1/VORDER);
  273.         break;
  274.       case TWO_D:
  275.         glDisable(GL_MAP1_VERTEX_4);
  276.         glEnable(GL_MAP2_VERTEX_4);
  277.         DrawPoints2();
  278.         DrawMapEval2(0.1/VMAJOR_ORDER,0.1/VMINOR_ORDER);
  279.         break;
  280.           default:
  281.             break;
  282.     }
  283.     break;
  284.       case MESH:
  285.     switch (arrayType) {
  286.       case ONE_D:
  287.         DrawPoints1();
  288.         glDisable(GL_MAP2_VERTEX_4);
  289.         glEnable (GL_MAP1_VERTEX_4);
  290.         glColor3f(0.0, 0.0, 1.0);
  291.         glMapGrid1d(40, 0.0, 1.0);
  292.         if (mapPoint) {
  293.         glPointSize(2);
  294.         glEvalMesh1(GL_POINT, 0, 40);
  295.         } else {
  296.         glEvalMesh1(GL_LINE, 0, 40);
  297.         }
  298.         break;
  299.       case TWO_D:
  300.         DrawPoints2();
  301.         glDisable(GL_MAP1_VERTEX_4);
  302.         glEnable(GL_MAP2_VERTEX_4);
  303.         glColor3f(0.0, 0.0, 1.0);
  304.         glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0);
  305.         if (mapPoint) {
  306.         glPointSize(2);
  307.         glEvalMesh2(GL_POINT, 0, 20, 0, 20);
  308.         } else if (polygonFilled) {
  309.         glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  310.         } else {
  311.         glEvalMesh2(GL_LINE, 0, 20, 0, 20);
  312.         }
  313.         break;
  314.           default:
  315.             break;
  316.     }
  317.     break;
  318.       default:
  319.         break;
  320.     }
  321. }
  322.  
  323. static void Reshape(int width, int height)
  324. {
  325.  
  326.     glViewport(0, 0, (GLint)width, (GLint)height);
  327.  
  328.     glMatrixMode(GL_PROJECTION);
  329.     glLoadIdentity();
  330.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0);
  331.     glMatrixMode(GL_MODELVIEW);
  332. }
  333.  
  334. static GLenum Key(int key, GLenum mask)
  335. {
  336.  
  337.     switch (key) {
  338.       case TK_ESCAPE:
  339.     tkQuit();
  340.       case TK_LEFT:
  341.     rotY -= 30;
  342.     break;
  343.       case TK_RIGHT:
  344.     rotY += 30;
  345.     break;
  346.       case TK_UP:
  347.     rotX -= 30;
  348.     break;
  349.       case TK_DOWN:
  350.     rotX += 30;
  351.     break;
  352.       case TK_1:
  353.     arrayType = ONE_D;
  354.     break;
  355.       case TK_2:
  356.     arrayType = TWO_D;
  357.     break;
  358.       case TK_e:
  359.     mapType = EVAL;
  360.     break;
  361.       case TK_m:
  362.     mapType = MESH;
  363.     break;
  364.       case TK_f:
  365.     polygonFilled = !polygonFilled;
  366.     break;
  367.       case TK_p:
  368.     mapPoint = !mapPoint;
  369.     break;
  370.       case TK_c:
  371.     colorType = !colorType;
  372.     break;
  373.       case TK_t:
  374.     textureType = !textureType;
  375.     break;
  376.       case TK_l:
  377.     lighting =! lighting;
  378.     if (lighting) {
  379.         glEnable(GL_LIGHTING);
  380.         glEnable(GL_LIGHT0);
  381.         glEnable(GL_AUTO_NORMAL);
  382.     } else {
  383.         glDisable(GL_LIGHTING);
  384.         glDisable(GL_LIGHT0);
  385.         glDisable(GL_AUTO_NORMAL);
  386.     }
  387.     break;
  388.       default:
  389.     return GL_FALSE;
  390.     }
  391.     return GL_TRUE;
  392. }
  393.  
  394. static void Draw(void)
  395. {
  396.  
  397.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  398.  
  399.     glPushMatrix();
  400.  
  401.     glTranslatef(0.0, 0.0 , translateZ);
  402.     glRotatef(rotX, 1, 0, 0);
  403.     glRotatef(rotY, 0, 1, 0);
  404.     RenderEval();
  405.  
  406.     glPopMatrix();
  407.  
  408.     glFlush();
  409.  
  410.     if (doubleBuffer) {
  411.     tkSwapBuffers();
  412.     }
  413. }
  414.  
  415. static GLenum Args(int argc, char **argv)
  416. {
  417.     GLint i;
  418.  
  419.     doubleBuffer = GL_FALSE;
  420.     directRender = GL_TRUE;
  421.  
  422.     for (i = 1; i < argc; i++) {
  423.     if (strcmp(argv[i], "-sb") == 0) {
  424.         doubleBuffer = GL_FALSE;
  425.     } else if (strcmp(argv[i], "-db") == 0) {
  426.         doubleBuffer = GL_TRUE;
  427.     } else if (strcmp(argv[i], "-dr") == 0) {
  428.         directRender = GL_TRUE;
  429.     } else if (strcmp(argv[i], "-ir") == 0) {
  430.         directRender = GL_FALSE;
  431.     } else {
  432.         printf("%s (Bad option).\n", argv[i]);
  433.         return GL_FALSE;
  434.     }
  435.     }
  436.     return GL_TRUE;
  437. }
  438.  
  439. void main(int argc, char **argv)
  440. {
  441.     GLenum type;
  442.  
  443.     if (Args(argc, argv) == GL_FALSE) {
  444.     tkQuit();
  445.     }
  446.  
  447.     tkInitPosition(0, 0, 300, 300);
  448.  
  449.     type = TK_RGB | TK_DEPTH;
  450.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  451.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  452.     tkInitDisplayMode(type);
  453.  
  454.     if (tkInitWindow("Evaluator Test") == GL_FALSE) {
  455.     tkQuit();
  456.     }
  457.  
  458.     Init();
  459.  
  460.     tkExposeFunc(Reshape);
  461.     tkReshapeFunc(Reshape);
  462.     tkKeyDownFunc(Key);
  463.     tkDisplayFunc(Draw);
  464.     tkExec();
  465. }
  466.